//	ID : pallab81
// 	PROG : 11747
// 	LANG : C++
// 	2011-09-10-16.35.19 Saturday
//
// 	"I have not failed, I have just found 10000 ways that won't work.
//


#include <iostream>
#include <algorithm>
#include <vector>
#include <sstream>
#include <fstream>
#include <string>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>

#include <ctime>
#include <cassert>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <climits>
#include <cstring>
#include <cstdlib>

using namespace std;

#define VI vector< int >
#define CI( x ) scanf("%d",&x)
#define CL( x ) scanf("%lld",&x)
#define CD( x ) scanf("%lf",&x )
#define fo(i,st,ed) for(int i = st ; i < ed ; ++i )
#define foE(i,st,ed) for(int i = st ; i <= ed ; ++i )
#define foit(i, x) for (typeof((x).begin()) i = (x).begin(); i != (x).end(); i++)
#define bip system("pause")
#define Int long long
#define pb push_back
#define SZ(X) (int)(X).size()
#define LN(X) (int)(X).length()
#define mk make_pair
#define f first
#define s second
#define SET( ARRAY , VALUE ) memset( ARRAY , VALUE , sizeof(ARRAY) )

inline void wait( double seconds ) {
    double endtime = clock()+( seconds* CLOCKS_PER_SEC );
    while( clock() < endtime ) {
        ;
    }
}
// disjoint set start
const int inf = (int)1e6;
int root[inf+10];
int rank[inf+10];

inline void _Set() {
    fo(i,0,inf) {
        root[i]=i;
        rank[i]=0;
    }
}
inline int _Find(int x) {
    if( x!= root[x] ) {
        root[x] = _Find( root[x] );
    }
    return root[x];
}
inline void _Union(int x, int y) {
    int px = _Find(x);
    int py = _Find(y);
    if( rank[px] > rank[py] ) {
        root[py] = px;
    } else {
        root[px] = py;
    }
    if( rank[px] == rank[py]) {
        rank[py]+=1;
    }
}
// disjoint set end

int node,edge;
set< pair<int, pair<int,int> >, less< pair<int, pair<int,int> > > > pq;

inline void Read() {
    pq.clear();
    int x,y,c;
    fo(i,0,edge) {
        CI(x);
        CI(y);
        CI(c);
        pq.insert( mk(c, mk(x,y) ) );
    }
}

inline void Proc() {
    _Set();
    int n=0;
    vector<int> to_show;
    while( pq.empty()==false ) {
        pair<int, pair<int,int> >temp =  (*pq.begin());
        pq.erase(pq.begin());
        int c = temp.f;
        int sx = temp.s.f;
        int sy = temp.s.s;

        if( _Find(sx)==_Find(sy) ) {
            to_show.pb(c);
        } else {
            _Union(sx,sy);
        }
    }

    if(to_show.empty()) {
        cout<<"forest";
    } else {
        string ss="";
        foit(i,to_show) {
            cout<<ss<<(*i);
            ss=" ";
        }
    }
    cout<<"\n";

}
int main() {
//    #ifndef ONLINE_JUDGE
//        freopen("in.txt","rt",stdin);
//    #endif
//
    while( scanf("%d %d",&node,&edge)==2 ) {
        if(node==0 && edge==0)break;
        Read();
        Proc();
    }

    return 0;
}
